﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Xml.Schema;
using System.IO;
using System.Xml;

namespace wscf.lightblue.UI
{
	public partial class WsdlGenerationDialog : Form
	{
		public WsdlGenerationOptions Options { get; set; }

		public WsdlGenerationDialog(WsdlGenerationOptions options)
		{
			InitializeComponent();
			Options = options;
		}

		private void searchMetadataFilesButton_Click(object sender, EventArgs e)
		{
			if (metadatFilesOpenFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				metadataFilesTextBox.Text = string.Join<string>(";", metadatFilesOpenFileDialog.FileNames);
			}
		}

		private void searchImportHeaderSchemaButton_Click(object sender, EventArgs e)
		{
			if (importHeaderSchemaOpenFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				importHeaderSchemaTextbox.Text = string.Join<string>(";", importHeaderSchemaOpenFileDialog.FileNames);
			}
		}

		private void cancelButton_Click(object sender, EventArgs e)
		{
			Close();
		}

		private void LoadControls()
		{
			metadataFilesTextBox.Text = Options.MetadataFiles;
			serviceNameTextbox.Text = Options.ServiceName;
			destinationNamespaceTextBox.Text = Options.TargetNamespace;
			destinationFileNameTextBox.Text = Options.OutputFile;
			destinationNamespaceTextBox.Text = Options.TargetNamespace;

			importHeaderSchemaTextbox.Text =
				Properties.Settings.Default.ImportSchemas;
			UpdateHeaderElements();
			requestHeaderComboBox.Text = Properties.Settings.Default.SoapRequestHeader;
			responseHeaderComboBox.Text = Properties.Settings.Default.SoapResponseHeader;
			faultComboBox.Text = Properties.Settings.Default.SoapFault;
		}

		private void SaveControls()
		{
			Options.MetadataFiles = metadataFilesTextBox.Text;
			Options.ServiceName = serviceNameTextbox.Text;
			Options.TargetNamespace = destinationNamespaceTextBox.Text;
			Options.ImportSchemas = importHeaderSchemaTextbox.Text;
			Options.InferOperationsPattern = new Hashtable();
			if (!string.IsNullOrEmpty(inferOperationsTextBox.Text))
			{
				string[] arr = inferOperationsTextBox.Text.Split(';');
				for(int i = 0; i < arr.Length;)
				{
					Options.InferOperationsPattern.Add(arr[i++], arr[i++]);
				}
			}
			Options.OutputFile = destinationFileNameTextBox.Text;
			if (requestHeaderComboBox.SelectedItem != null)
			{
				Options.RequestHeaderElement = requestHeaderComboBox.SelectedItem.ToString();
			}
			if (responseHeaderComboBox.SelectedItem != null)
			{
				Options.ResponseHeaderElement = responseHeaderComboBox.SelectedItem.ToString();
			}
			if (faultComboBox.SelectedItem != null)
			{
				Options.FaultElement = faultComboBox.SelectedItem.ToString();
			}

			Properties.Settings.Default.ImportSchemas = importHeaderSchemaTextbox.Text;
			if (requestHeaderComboBox.SelectedItem != null)
			{
				Properties.Settings.Default.SoapRequestHeader = requestHeaderComboBox.SelectedItem.ToString();
			}
			else
			{
				Properties.Settings.Default.SoapRequestHeader = "";
			}
			if (responseHeaderComboBox.SelectedItem != null)
			{
				Properties.Settings.Default.SoapResponseHeader = responseHeaderComboBox.SelectedItem.ToString();
			}
			else
			{
				Properties.Settings.Default.SoapResponseHeader = "";
			}
			if (faultComboBox.SelectedItem != null)
			{
				Properties.Settings.Default.SoapFault = faultComboBox.SelectedItem.ToString();
			}
			else
			{
				Properties.Settings.Default.SoapFault = "";
			}
			Properties.Settings.Default.Save();
		}

		private void generateButton_Click(object sender, EventArgs e)
		{
			SaveControls();
			Close();
		}

		private void WsdlGenerationDialog_Load(object sender, EventArgs e)
		{
			LoadControls();
		}

		private void importHeaderSchemaTextbox_TextChanged(object sender, EventArgs e)
		{
			UpdateHeaderElements();
		}

		private void UpdateHeaderElements()
		{
			try
			{
				XmlSchemaSet set = new XmlSchemaSet();

				List<string> schemas = new List<string>();
				if (string.IsNullOrEmpty(importHeaderSchemaTextbox.Text))
				{
					schemas.AddRange(metadataFilesTextBox.Text.Split(';'));
				}
				else
				{
					schemas.AddRange(importHeaderSchemaTextbox.Text.Split(';'));
				}

				foreach (string schema in schemas)
				{
					if (File.Exists(schema.Trim()))
					{
						set.Add(XmlSchema.Read(XmlReader.Create(schema), null));
					}
				}
				set.Compile();

				requestHeaderComboBox.Items.Clear();
				responseHeaderComboBox.Items.Clear();
				faultComboBox.Items.Clear();
				foreach (XmlSchemaElement obj in set.GlobalElements.Values)
				{
					requestHeaderComboBox.Items.Add(obj.Name);
					responseHeaderComboBox.Items.Add(obj.Name);
					faultComboBox.Items.Add(obj.Name);
				}
				requestHeaderComboBox.Items.Add(string.Empty);
				responseHeaderComboBox.Items.Add(string.Empty);
				faultComboBox.Items.Add(string.Empty);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "Error reading schema", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

	}
}
